package com.lkd.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.domain.Node;
import com.lkd.domain.User;
import com.lkd.mapper.NodeMapper;
import com.lkd.pageParam.RegionDetailsPageParam;
import com.lkd.pageParam.RegionPageParam;
import com.lkd.service.IBusinessService;
import com.lkd.service.INodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.service.IRegionService;
import com.lkd.vo.NodeDetailsVo;
import com.lkd.vo.RegionDetailsVo;
import com.lkd.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-10
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements INodeService {
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private IRegionService regionService;
    @Autowired
    IBusinessService businessService;
    @Override
    public Page<RegionDetailsVo> getRegionDetailsVoPage(RegionDetailsPageParam param) {

        //查询点位的机器数量 从createTime到vmCount
        List<RegionDetailsVo>  list =   nodeMapper.getMvCountOfNode(param.getRegionId());
        list.stream().forEach(regionDetailsVo -> {
            //根据regionId查询region名字
            String name = regionService.getById(regionDetailsVo.getRegionId()).getName();
            RegionPageParam regionPageParam = new RegionPageParam();
            regionPageParam.setName(name);
            Page<RegionVo> page = regionService.getPage(regionPageParam);
            List<RegionVo> records = page.getRecords();
            if (!CollectionUtils.isEmpty(records)) {
                regionDetailsVo.setRegion(records.get(0));
            }
            regionDetailsVo.setBusinessType(businessService.getById(regionDetailsVo.getBusinessId()));
        });

      Page<RegionDetailsVo> page = new Page<>(param.getPageIndex(), param.getPageSize());
//        List<RegionDetailsVo> nodes = nodeMapper.selectDetailsList(param.getRegionId());
        page.setSize(param.getPageSize()).setRecords(list).setTotal(list.size()).setPages(param.getPageIndex());
        return page;
    }

    @Override
    public List<NodeDetailsVo> getVmlistOfOneNode(Long nodeId) {
        //查询当前点位的所有设备
        List<NodeDetailsVo> machines  =  nodeMapper.selectAllMachine(nodeId);
        for (NodeDetailsVo nodeDetailsVo : machines) {
            nodeDetailsVo.getRegion().setNodeCount(nodeDetailsVo.getRegion().getNodeList().size());
            List<RegionDetailsVo> regionDetailsVos = nodeMapper.selectDetailsList(nodeDetailsVo.getRegionId());
            for (RegionDetailsVo regionDetailsVo : regionDetailsVos) {
                for (Node node : regionDetailsVo.getRegion().getNodeList()) {
                    Long nodeId1 = node.getId();
                    if (nodeId1.equals(nodeDetailsVo.getNodeId())){
                        nodeDetailsVo.setNode(regionDetailsVo);
                    }
                }
            }
        }

        return machines;
    }

    @Override
    public Integer getNodeCountOfOneUser(Long userId) {
        Integer count =   nodeMapper.getNodeCount(userId);
        return count;
    }
}
